tree grammar EquationWalker;

options {
  language = Java;
  tokenVocab = EquationGrammar;
  ASTLabelType = CommonTree;
}

@header {
  package com.sri.ai.lpitools.latex.server;
}

converter returns [String result]
	:	c=expression EOF { result = c; }
	;

expression returns [String result]
	:	^('+' op1=expression op2=expression) { result = op1 + "+" + op2; }
	|	^('-' op1=expression op2=expression) { result = op1 + "-" + op2; }
	|	^('*' op1=expression op2=expression) { result = op1 + "*" + op2; }
	|	^('/' op1=expression op2=expression) { result = "\\frac{" + op1 + "}{" + op2 + "}";}
	|	^('%' op1=expression op2=expression) { result = op1 + "\\mod{" + op2 + "}";}
	|	^('^' op1=expression op2=expression) { result = "{" + op1 + "}^{" + op2 + "}";}
	|	IDENT { result = $IDENT.text; }
	|	FLOAT {	result = Double.toString(Double.parseDouble($FLOAT.text)); }
	|	INTEGER { result = Integer.toString(Integer.parseInt($INTEGER.text)); }
	|	^('|' e=expression '|') { result = "|" + e +"|"; }
	|	^('(' e=expression ')') { result = "(" + e + ")";}
	|	^(FUNCTION IDENT e=expression) { result = $IDENT.text + "(" + e + ")"; }
	|	^('=' op1=expression op2 = expression) { result = op1 + "\\ = " + op2; }
	|	^('!=' op1=expression op2 = expression) { result = op1 + "\\ != " + op2; }
	|	^('and' op1=expression op2 = expression) { result = op1 + " and " + op2; }
	|	^( CONDITION a=expression e1=expression e2=expression ) { result = "if\\ " + a + "\\ then\\ " + e1 + "\\ else\\ " + e2; }
	| ^( ATOMIC_RULE e=expression v=expression ) { result= e + " " + v; }
	;
